The PIC microcontroller's ADC works by what is called successive approximation. This is a hardware equivalent of the "Higher Lower" game that you can play to guess numbers. The idea is that you are trying to find the number the other person has thought of. By asking if the other player if their number is higher or lower than certain values you can quickly zero in to the value. For example, we know the number is in the range 0 to 1023. We start by asking "Is the number bigger than 512?" If the answer is no we can discount all the values between 512 and 1023. Our next question is "Is the number bigger than 255?", and so on until we get to the correct answer. It takes a surprisingly small number of questions to get to the answer, in this case no more than 10 are required.

When your program asks the hardware to start a conversion it first grabs a sample of the signal and stores this in a capacitor. This is so changes in the value of the voltage during conversion will not affect the result. The conversion then takes place as a number of steps. The speed at which the successive steps are taken affects the accuracy of the result; so the PIC microcontroller can be configured to convert at different speeds.

It is also possible to perform the conversion whilst the PIC microcontroller is in sleep mode. This is not a stupid as it sounds, in that the PIC microcontroller can be woken up when the conversion is complete; and if the conversion is made during sleep there is less chance of electrical noise made by the PIC microcontroller interfering with the signal you are reading. To do this requires slightly more complicated hardware, but in some situations it is necessary.

The important thing for us to remember is that the conversion process is not instantaneous. Our program will have to start the conversion process off and then wait for it to complete. We have already seen examples of situations where our programs have had to wait for a response from the outside world, or from slower hardware; for example writing to the EEPROM memory in the PIC microcontroller.

If you wish you can request that the ADC circuitry generate an interrupt when the conversion is complete. This makes it possible for your programs to do some other work whilst the signals are sampled. For the purpose of our program we are simply going to wait for the result rather than using interrupts.